// function flattening(arr) {
//   if(!Array.isArray(arr)) return;
//   return arr.reduce((a, b) => a.concat(Array.isArray(b) ? flattening(b) : b), []);
// }

function flattening(arr) {
  if(!Array.isArray(arr)) return;
  const stack = [...arr];
  const res = []
  while(stack.length) {
    const value = stack.shift();
    Array.isArray(value) ? stack.push(...value) : res.push(value);
  }
  return res;
}


const arr1 = [1, [2], [3, [4]], [5, [6, [7]]]];
const arr2 = [];
console.log(flattening(arr1));